package com.xmall.common.config;

import cn.hutool.core.util.StrUtil;
import com.xmall.common.base.BaseEntity;
import com.xmall.common.model.JwtUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.HashMap;

public class PojoUtils {
    public static void setSysProperties(BaseEntity beanDto, PlatformContant.OpType type) {
        Assert.notNull(beanDto);
        String userId = "1";
        String userIp = "127.0.0.1";

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Object principal = authentication.getPrincipal();

        new HashMap();
        if (principal!=null) {
            JwtUser jwtUser = (JwtUser) principal;
            if (jwtUser != null) {
                if (!StrUtil.hasEmpty(jwtUser.getId())) {
                    userId = jwtUser.getId();
                }
            }
        }

        switch (type) {
            case insert:
                beanDto.setCreateBy(userId);
                beanDto.setCreationDate(new Date());
                beanDto.setLastUpdateDate(new Date());
                beanDto.setLastUpdateBy(userId);
                beanDto.setLastUpdateIp(userIp);
//                beanDto.setVersion(0L);
                break;
            case update:
                beanDto.setLastUpdateDate(new Date());
                beanDto.setLastUpdateBy(userId);
                beanDto.setLastUpdateIp(userIp);
        }

    }
}
